package com.family.gmall.product.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.family.gmall.common.result.Result;
import com.family.gmall.model.product.entity.SkuInfo;
import com.family.gmall.product.service.SkuManageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @Auther: family
 * @Date: 2023/7/12 09:00
 * @Description:
 */
@Api(tags = "SKU")
@RestController
@RequestMapping("/admin/product")
public class SkuManageController {

    @Resource
    private SkuManageService skuManageService;

    @ApiOperation("根据spuId获取图片列表")
    @ApiImplicitParam(name = "spuId", value = "spuId", required = true)
    @GetMapping("spuImageList/{spuId}")
    public Result getSpuImageList(@PathVariable Long spuId) {
        return Result.ok(skuManageService.getSpuImageList(spuId));
    }

    @ApiOperation("根据spuId获取销售属性")
    @ApiImplicitParam(name = "spuId", value = "spuId", required = true)
    @GetMapping("/spuSaleAttrList/{spuId}")
    public Result getSpuSaleAttrList(@PathVariable Long spuId) {
        return Result.ok(skuManageService.getSpuSaleAttrList(spuId));
    }

    @ApiOperation("添加sku")
    @ApiImplicitParam(name = "skuInfo", value = "skuInfo", required = true)
    @PostMapping("/saveSkuInfo")
    public Result saveSkuInfo(@RequestBody SkuInfo skuInfo) {
        skuManageService.saveSkuInfo(skuInfo);
        return Result.ok();
    }

    @ApiOperation("获取sku分页列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "page", required = true),
            @ApiImplicitParam(name = "limit", value = "limit", required = true)

    })
    @GetMapping("/list/{page}/{limit}")
    public Result getList(@PathVariable Long page, @PathVariable Long limit) {
        Page<SkuInfo> skuInfoPage = new Page<>(page, limit);
        skuManageService.getPageList(skuInfoPage);
        return Result.ok(skuInfoPage);
    }

    @ApiOperation("上架")
    @ApiImplicitParam(name = "skuId", value = "skuId", required = true)
    @GetMapping("/onSale/{skuId}")
    public Result onSale(@PathVariable Long skuId) {
        skuManageService.onSale(skuId);
        return Result.ok();
    }

    @ApiOperation("下架")
    @ApiImplicitParam(name = "skuId", value = "skuId", required = true)
    @GetMapping("/cancelSale/{skuId}")
    public Result cancelSale(@PathVariable Long skuId) {
        skuManageService.cancelSale(skuId);
        return Result.ok();
    }
}
